package cisdi.mes.sm.rm.daoImpl;

import java.util.Date;

import cisdi.mes.sm.rm.dao.MeasureDocDao;
import cisdi.mes.sm.rm.domain.SmRmMeasureDoc;
import cisdi.mes.sm.util.base.Arith;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

@Repository
public class MeasureDocDaoImpl extends HibernateDaoSupport implements MeasureDocDao {

	@Autowired
	public void setMySessionFactory(SessionFactory sessionFactory){
		super.setSessionFactory(sessionFactory);
	}
	
	@Override
	public SmRmMeasureDoc getById(String measureId) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().get(SmRmMeasureDoc.class, measureId);
	}

	@Override
	public void update(SmRmMeasureDoc measureDoc) {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().update(measureDoc);
	}

	@Override
	public void inputRcv(SmRmMeasureDoc userDoc) {
		SmRmMeasureDoc md = new SmRmMeasureDoc();

		// 设置计量单、进厂类型、物料数据
		md.setMeasureId(userDoc.getMeasureId());
		md.setDeliveryType(userDoc.getDeliveryType());
		md.setMaterialId(userDoc.getMaterialId());
		md.setMaterialDesc(userDoc.getMaterialDesc());

		// 设置重量数据
		md.setNetWeight(userDoc.getNetWeight());
		md.setGrossWeight(userDoc.getGrossWeight());
		md.setTareWeight(userDoc.getTareWeight());
		md.setNetWeight(userDoc.getNetWeight());
		md.setSurplusNetWeight(userDoc.getNetWeight());
		
		// 更改收货状态
		md.setStatus("RECEIVE");
		md.setReceiveTime(userDoc.getReceiveTime());
		md.setQualityBatch(userDoc.getQualityBatch());
		md.setAnaDecide("N");

		this.getHibernateTemplate().save(md);
	}

	@Override
	public void matRcv(SmRmMeasureDoc userDoc) {
		SmRmMeasureDoc md = getById(userDoc.getMeasureId());

		// 更改收货状态
		md.setStatus("RECEIVE");
		md.setReceiveTime(new Date());

		// 设置重量-净重
		Double netWeight = userDoc.getNetWeight();
		Double grossWgt = md.getGrossWeight();
		if (grossWgt != null && grossWgt > 0 && md.getTareWeight() != null) {
			netWeight = Arith.sub(md.getGrossWeight(), md.getTareWeight());
			if (md.getDeductWeight() != null) {
				netWeight = Arith.sub(netWeight, md.getDeductWeight());
			}
			if (md.getSecondDeductWeight() != null) {
				netWeight = Arith.sub(netWeight, md.getSecondDeductWeight());
			}
		}
		md.setNetWeight(netWeight);
		md.setSurplusNetWeight(netWeight);
		md.setQualityBatch(userDoc.getQualityBatch());
		md.setAnaDecide("N");

		this.getHibernateTemplate().update(md);
	}

	@Override
	public void matRtn(String measureId) {
		SmRmMeasureDoc md = getById(measureId);
		md.setStatus("RETURN");
		this.getHibernateTemplate().update(md);
	}

	@Override
	public void matUnload(SmRmMeasureDoc md, Double wgt) {
		// 设置入库重量
		Double surplusNetWeight = md.getSurplusNetWeight();
		if (surplusNetWeight < wgt)
			surplusNetWeight = 0d;
		surplusNetWeight = Arith.sub(surplusNetWeight, wgt);
		md.setSurplusNetWeight(surplusNetWeight);

		this.getHibernateTemplate().update(md);
	}

	@Override
	public void decreaseStorageWgt(SmRmMeasureDoc md, Double wgt) {
		Double storageWgt = md.getStorageWgt();
		storageWgt = Arith.sub(storageWgt, wgt);
		if (storageWgt < 0) {
			storageWgt = 0d;
		}
		md.setStorageWgt(storageWgt);
		this.getHibernateTemplate().update(md);

	}

	@Override
	public SmRmMeasureDoc getByQualityBatch(String qualityBatch) {
		String hql = "from SmRmMeasureDoc t where t.qualityBatch=?";
		return (SmRmMeasureDoc)this.getHibernateTemplate().find(hql, qualityBatch).get(0);
	}

}
